﻿$(function () {
    $("body").css("height", height + "px");
    $(".login_bg").css("height", height + "px");
    if (isMobile.Android()) {
        document.addEventListener("deviceready", onDeviceReady, false);
    }
    else {
        onDeviceReady();
    }
    function onDeviceReady() {
        //   隐藏启动画面
        Ready();
        window.localStorage.index = 0;
        //  navigator.splashscreen.hide();
        //   点击登录按钮事件
        if (window.localStorage.CA_Name) {
            $("#Password").focus();
            $("#Name").val(window.localStorage.CA_Name);
        }
        else if (window.localStorage.userName) {
            $("#Password").focus();
            $("#Name").val(window.localStorage.userName);
        }
        else {
            $("#Name").focus();
        }
        $(".user_input").on(touch_e, function () {
            $("#Name").focus();
        });
        $(".paas_input").on(touch_e, function () {
            $("#Password").focus();
        });
		update(function (dat) {
			/*var version = dat.versionName;
            window.plugins.getIntent.getVersion([], function (text) {
              if (text < version) {
				navigator.notification.confirm("发现新版本\n当前版本：" + text + "\n" + '服务器版本:' + dat.versionName + "\n" + dat.updateInfo,
                showConfirm,
                '版本更新',
                ['确认更新']);
                function showConfirm(button) {
                    if (button == 1) {
                        var url = serverURL + dat.apkURL;
                        window.openF(url, "DaTong.apk");
                    }
                }
              }
            });*/
		   }, function () {});
        // 点击登录按钮
        $(".login_button").on(touch_e, function () {
            var user = $("#Name").val();
            var pass = $("#Password").val();
            pass = $.md5("dtoa"+pass)
            console.log(user, pass);
            var self = {
                userName: user,
                password: pass,
                client: "PHONE"
            };
            var url = serverURL + "cmcp/doLogin";
            if (user == '请输入用户名' || pass == '' || user == '') {
                Alert("请输入用户名或者密码");
            }
//            else if (user != window.localStorage.CA_Name) {
//				Alert("用户名不正确");
//            }
            else {
                $.ajax({
                    url: url,
                    type: "POST",
                    async: true,
                    timeout: 20000,
                    data: {"userName": user, "password": pass, client: "PHONE"},
                    success: function (Data, textStatus, XMLhttpRequest) {
                        console.log(Data);
                        window.plugins.getIntent.hide();
                        window.localStorage.state = "true";
                      
                        if (Data == null || Data == '' || Data == "false") {
                        	 // 提示错误信息
                            Alert("登录失败");
                        }else {
                        	console.log("登录成功==>"+Data);
                        	  //   登录成功
                        	var sid = Data.split("__")[0];
                        	var xtechAth = Data.split("__")[1];
                        	window.localStorage.setItem("sid",sid);
                        	window.localStorage.setItem("xtechAth",xtechAth);
                            window.localStorage.userName = user;
                            window.localStorage.passWord = pass;
							Jump(sid,xtechAth);
                        }
                    },
                    error: function (a, b) {
                        // 提示错误信息
                        console.log("err " + b);
                        Alert("登录失败");
                        //  window.plugins.getIntent.hide();

                    }
                });
            }
        });


        document.addEventListener("backbutton", onBackKeyDown, false);
        // Android返回键动作
        function onBackKeyDown() {
            navigator.notification.confirm('您确定要退出程序吗？', showConfirm, '退出程序', ['确定', '取消']);
            function showConfirm(button) {
                if (button == 1) {
                	//document.removeEventListener("backbutton", onBackKeyDown, false);//注销返回键
                    LoginOut();
                }
            }

        }

        function Jump(sid,xtechAth) {
            var href = location.href;
            var len = href.lastIndexOf("/");
            var url = href.substr(0, len - 5) + "dbrw/dbrw.html?sid="+sid+"&xtechAth="+xtechAth;
            window.plugins.getIntent.hide();
            window.openView(url);
            window.closeView();
        }
    }
});